package com.qf.fileupload.controller;

import com.qf.fileupload.vo.HelloVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
public class UploadController {

    @Value("${qfsy.fileupload.path}")
    String filepath;

    @RequestMapping("/test1")
    public HelloVo test1(@Valid HelloVo vo){
        System.out.println(vo);
        return vo;
    }


    @PostMapping("/upload")
    public String upload(MultipartFile file,MultipartFile file2) throws IOException {

        //1. 生产一个唯一的文件名
        final String prefix = UUID.randomUUID().toString();
        final String originalFilename = file.getOriginalFilename();
        //2. 拼装文件在服务器端的绝对路径
        final String finalFilepath = filepath + prefix + "_" + originalFilename;

        int a= 1/0;
        //3. 存放文件
        file.transferTo(new File(finalFilepath));

        return "ok";

    }
}
